<?php
/**
 * This file is part of PHPlaza (xhttp://code.google.com/p/phplaza)
 * This file is licensed under the GNU Lesser General Public License
 *      http://www.gnu.org/licenses/lgpl.html
 *
 * No warranty is given, express or implied. Use at your own risk.
 */

namespace core;
use core::collections as coll;
use core::components as comp;

abstract class Page extends comp::Component
{
	private $pageData;
	private $components;
	
	public function __construct()
	{
		parent::__construct();
		
		$this->pageData = new coll::CList();
		$this->components = new coll::CList();
	}
	
	public function __get($index)
	{
		return $this->pageData[$index];
	}
	
	public function __set($index, $value)
	{
		$this->pageData[$index] = $value;
	}
	
	public function addChild(coll::ITreeItem $item)
	{
		if (! ($item instanceof comp::Component))
		{
			throw new Exception('Only components can be added as children to a page');
		}
		
		if (isset($this->components[$item->getId()]))
		{
			throw new Exception('Component with id "' . $item->getId() . '" already added to page');
		}
		
		parent::addChild($item);
		$this->components[$item->getId()] = $item;
	}
	
	public function __isset($index)
	{
		return isset($this->pageData[$index]);
	}
	
	public function getComponents()
	{
		return $this->components;
	}
	
	public function addComponent(core::components::Component $component)
	{
		$this->components[$component->getId()] = $component;
	}
	
	public function _getState(core::components::State $state)
	{
		$state->__Components = $this->components;
		$state->__PageData = $this->pageData;
	}
	
	public function _setState(core::components::State $state)
	{
		$this->components = $state->__Components;
		$this->pageData = $state->__PageData;
	}
	
	// lifecycle methods
	public function initialize()
	{
	}
	
	
}